<?php
namespace app\index\controller;


use app\common\controller\Base;
use think\facade\Request;
use app\common\model\User as UserModel;
use think\facade\Session;
use think\captcha\Captcha;

class User extends Base
{
    /*
     * 生成验证码
     */
    public function verify()
    {
        $config = [
            'length'    =>  4,
            'useNoise'  =>  true,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    /*
     * 会员注册
     */
    public function register()
    {
        $this->assign('title','注册');
        return $this->fetch('register');
    }

    /*
     * 注册会员操作
     */
    public function registerDo(UserModel $userModel)
    {
        if (Request::isAjax()) {

            $params = Request::param();
            $rule = 'app\index\validate\Register';
            $result = $this->validate($params, $rule);

            if (true !== $result) {
                return ['status'=>-1,'message'=>$result,'token'=>Request::token()];
            }

            $res = $userModel->addUser($params);

            if ($res) {
                $info = $userModel->getInfoById($res->id);

                $user = [
                    'id'    =>  $info['id'],
                    'email' =>  $info['email'],
                ];

                Session::set('user',$user);

                return ['status'=>1,'message'=>'注册成功'];
            }

            return ['status'=>0,'message'=>'注册失败','token'=>Request::token()];
        } else {
            $this->error('请求类型错误','register');
        }
    }

    /*
     * 会员登录
     */
    public function login()
    {
        return $this->fetch('login');
    }

    /*
     * 会员登录操作
     */
    public function loginDo(UserModel $userModel)
    {
        if (Request::isAjax()) {

            $params = Request::param();
            $rule = 'app\index\validate\Login';
            $result = $this->validate($params, $rule);

            if (true !== $result) {
                return ['status'=>-1,'message'=>$result,'token'=>Request::token()];
            }

            $userInfo = $userModel->getInfoByEmail($params['email']);

            if (null == $userInfo) {
                return ['status'=>0,'message'=>'邮箱不存在或密码错误','token'=>Request::token()];
            }

            if ($userInfo['password'] != md5($params['password'])) {
                return ['status'=>-2,'message'=>'邮箱不存在或密码错误','token'=>Request::token()];
            }

            $user = [
                'id'    =>  $userInfo->id,
                'email' =>  $userInfo['email'],
            ];

            Session::set('user',$user);

            return ['status'=>1,'message'=>'登录成功'];

        } else {
            $this->error('请求类型错误','login');
        }
    }

    /*
     * 会员中心
     */
    public function index(UserModel $userModel)
    {
        $user = Session::get('user');

        $info = $userModel->getInfoById($user['id']);

        $this->assign('info',$info);
        $this->assign('title','会员中心');
        return $this->fetch('index');
    }

    /*
     * 个人资料编辑
     */
    public function profile(UserModel $userModel)
    {
        $user = Session::get('user');

        $info = $userModel->getInfoById($user['id']);

        $this->assign('info',$info);
        $this->assign('title','个人资料');
        return $this->fetch('profile');
    }

    /*
     * 编辑个人资料操作
     */
    public function profileDo(UserModel $userModel)
    {
        if (Request::isAjax()) {

            $params = Request::param();
            $rule = 'app\index\validate\Profile';
            $result = $this->validate($params, $rule);

            if (true !== $result) {
                return ['status'=>-1,'message'=>$result,'token'=>Request::token()];
            }

            $res = $userModel->editUser($params);

            if ($res) {
                return ['status'=>1,'message'=>$res,'token'=>Request::token()];
            }

            return ['status'=>0,'message'=>'更新失败','token'=>Request::token()];

        } else {
            $this->error('请求类型错误');
        }
    }

    /*
     * 修改密码
     */
    public function changePwd()
    {
        return $this->fetch('changePwd');
    }

    /*
     * 密码修改操作
     */
    public function changePwdDo(UserModel $userModel)
    {
        if (Request::isAjax()) {

            $parmas = Request::param();
            $rule = 'app\index\validate\Password';
            $result = $this->validate($parmas, $rule);

            if (true !== $result) {
                return ['status'=>-1,'message'=>$result,'token'=>Request::token()];
            }

            $userInfo = $userModel->getInfoById($parmas['id']);
            
            if ($userInfo['password'] != md5($parmas['old_password'])) {
                return ['status'=>0,'message'=>'旧密码错误','token'=>Request::token()];
            }

            $data = [
              'id'       =>  $parmas['id'],
              'password' =>  md5($parmas['new_password']),
            ];

            $res = $userModel->editUser($data);

            if ($res) {
                return ['status'=>1,'message'=>'修改成功','token'=>Request::token()];
            }

            return ['status'=>-2,'message'=>'修改失败','token'=>Request::token()];

        } else {
            $this->error('请求类型错误');
        }
    }

    /*
     * 头像修改操作
     */
    public function changeAvatar(UserModel $userModel)
    {
        if (Request::isAjax()) {

            //判断上传的图片不能为空
            if (empty(Request::file())) {
                return ['status'=>-1,'message'=>'图片不能为空'];
            }

            //删除id对应的图片
            $userModel->delAvatar(Request::param('id'));

            //获取上传的图片信息
            $file = Request::file('avatar');

            //图片信息验证与上传到服务器
            $info = $file->validate([
                'size'  =>  500000000,
                'ext'   =>  'jpeg,jpg,png,gif',
            ])->move('uploads/avatar/');

            if ($info) {
                $data['id'] = Request::param('id');
                $data['avatar'] = $info->getSaveName();
            } else {
                return ['status'=>0,'message'=>$file->getError()];
            }

            $res = $userModel->changeAvatar($data);

            if ($res) {
                return ['status'=>1,'message'=>'更新成功','img'=>$res['avatar']];
            }

            return ['status'=>-2,'message'=>'更新失败'];

        } else {
            $this->error('请求类型错误');
        }
    }

    /*
     * 注销登录
     */
    public function logout()
    {
        Session::clear();
        $this->success('注销成功','User/login');
    }


}